package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.Areas;
import com.monitor.entity.Cities;
import com.monitor.entity.Provinces;
import com.monitor.service.AreasService;
import com.monitor.service.CitiesService;
import com.monitor.service.ProvincesService;
import com.monitor.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 行政区域地州市信息表 前端控制器
 * </p>
 *
 * @author 
 * @since 2018-12-07
 */
@RestController
@RequestMapping("/cities")
public class CitiesController {
	@Autowired
	private CitiesService citiesService;
	
	@Autowired
	private ProvincesService provincesService;

	@Autowired
	private AreasService areasService;

	@RequestMapping("/getAllProvinces")
	public Result<List<Provinces>> getAllProvinces(){
		List<Provinces> list= provincesService.selectList(new EntityWrapper<Provinces>());
		return new Result<List<Provinces>>(list, list.size());
	}
	
	@RequestMapping("/selectByProId")
	public Result<List<Cities>> selectByProId(int proId){
        Result<List<Cities>> result=new Result<>();
		List<Cities> list=citiesService.selectList(new EntityWrapper<Cities>().eq("provinceid", proId));
        List<Cities> lists=new ArrayList<>();

		list.parallelStream().forEach(cities->{
            if (cities.getCity().equals("市辖区")||cities.getCity().equals("县")){
                List<Areas> city_id = areasService.selectList(new EntityWrapper<Areas>().eq("cityid", cities.getCityid()));
                city_id.parallelStream().forEach(city->{
                            Cities areas=new Cities();
                            areas.setCity(city.getArea());
                            lists.add(areas);

                        }

                );
            }

        }

        );
        if(lists.size()>0){
            result.setData(lists);
            result.setCount(lists.size());
        }else{
            result.setData(list);
            result.setCount(list.size());
        }


		return  result;
	}

}

